## ##

rm(list = ls())

##

library(tidyverse)
library(cowplot)
library(lubridate)

## Get data - partisanship

civ <- read_rds("data/civey_party_replication.rds")

## Monthly intervals

start <- as.Date("2021-01-01")
end <- as.Date("2021-10-01")

dates_m <- floor_date(seq(start, end, by = 'month'), 
                      unit = "month") + 11

## Aggregate to month level

civ_agg_month <- civ %>% 
  mutate(month_10 = cut(date, dates_m)) %>% 
  group_by(month_10, treat_categ) %>% 
  summarise(share = mean(green),
         n_tot = n()) %>% 
  ungroup() %>% 
  mutate(se = sqrt((share*(1-share)) / n_tot)) %>% 
  mutate(conf.low = share - 1.96*se,
         conf.high = share + 1.96*se) %>% 
  mutate(month_10 = as.numeric(as.factor(month_10)) - 7) %>% 
  mutate(treat_categ = dplyr::recode(treat_categ,
                                     'none' = 'Unaffected',
                                     'schwach' = 'Weakly affected',
                                     'schwer' = 'Highly affected')) %>% 
  mutate(treat_categ = factor(treat_categ, levels = unique(treat_categ)[c(3,2,1)]))

## Create first plot

p1 <- ggplot(civ_agg_month, aes(month_10, share * 100, treat_categ)) +
  geom_vline(xintercept = -0.5, color = 'black', linetype = 'dotted') +
  #geom_errorbar(aes(ymin = conf.low, ymax = conf.high, color = treat_categ)) +
  geom_point(aes(color = treat_categ)) +
  geom_line(aes(color = treat_categ)) +
  #geom_smooth(aes(color = treat_categ), se = F, method = 'loess', span = 0.2) +
  theme_bw() +
  xlab('Months relative to flood') +
  ylab('Green party support (%)') +
  scale_x_continuous(breaks = -10:10) +
  theme(legend.position = 'bottom') +
  scale_color_grey(name = 'Flood intensity') +
  ylim(10, 25)

## Most important issues - get data ##

civ_mip <- read_rds("data/civey_issue_replication.rds")

## Aggregate to month

mip_agg_month <- civ_mip %>% 
  mutate(month_10 = cut(date, dates_m)) %>% 
  group_by(month_10, treat_categ) %>% 
  summarise(climate = mean(climate, na.rm = T)) %>% 
  ungroup() %>% 
  mutate(month_10 = as.numeric(as.factor(month_10)) - 7) %>% 
  mutate(treat_categ = dplyr::recode(treat_categ,
                                     'none' = 'Unaffected',
                                     'schwach' = 'Weakly affected',
                                     'schwer' = 'Highly affected')) %>% 
  mutate(treat_categ = factor(treat_categ, levels = unique(treat_categ)[c(3,2,1)]))

## Create second plot

p2 <- ggplot(mip_agg_month, aes(month_10, climate * 100, treat_categ)) +
  geom_vline(xintercept = -0.5, color = 'black', linetype = 'dotted') +
  #geom_errorbar(aes(ymin = conf.low, ymax = conf.high, color = treat_categ)) +
  geom_point(aes(color = treat_categ)) +
  geom_line(aes(color = treat_categ)) +
  #geom_smooth(aes(color = treat_categ), se = F, method = 'loess', span = 0.2) +
  theme_bw() +
  xlab('Months relative to flood') +
  ylab('Climate change salience (%)') +
  scale_x_continuous(breaks = -10:10) +
  theme(legend.position = 'bottom') +
  scale_color_grey(name = 'Flood intensity') +
  ylim(10, 35)

## Combine plots ##

p2_edit <- p2 + 
  theme(legend.position = "none") + 
  theme(axis.title.x = element_blank())
p2_edit

cowplot::plot_grid(p2_edit, p1, ncol = 1, rel_heights = c(0.8, 1))
